package com.cos.shop.dao.impl;

import com.cos.shop.dao.ItemDao;
import com.cos.shop.entity.Item;
import com.cos.shop.util.DBUtils;
import com.cos.shop.vo.ItemProductVo;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author: COS
 * @time: 2023/5/12 9:23
 * @description:
 */
public class ItemDaoImpl implements ItemDao {
    private QueryRunner qr = new QueryRunner(DBUtils.getDataSource());

    @Override
    public List<ItemProductVo> getItemListByOid(String oid) throws SQLException {
        String sql ="select" +
                "  `i_id` 'iid'," +
                "  `o_id` 'oid'," +
                "  i.`p_id` 'pid'," +
                "  `i_count` 'icount'," +
                "  `i_num` 'inum' ," +
                "  `p_name` 'pname' ," +
                "  `p_time` 'ptime' ," +
                "  `p_price` 'pprice' ," +
                "  `p_image` 'pimage' ," +
                "  `p_state` 'pstate' " +
                " from item i inner join product p " +
                " on i.p_id=p.p_id where o_id =?";
        return qr.query(sql,new BeanListHandler<>(ItemProductVo.class),oid);
    }

    @Override
    public int[] addItem(Object[][] args) throws SQLException {
        //批量新增 只会1次链接
        return qr.batch(DBUtils.getConnection(),
                "insert into item values (default,?,?,?,?)",
                args);
    }
}
